package Servlets;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import MD5_Encryption.Encrypt;
import QueryExcecution.CheckLogin;
import beanUtils.BeanUtility;
import beans.LoginFormBean;

public class CustomerLogin extends HttpServlet{
	LoginFormBean lfb=new LoginFormBean();
	Encrypt e=new Encrypt();
	String fname,lname;
	CheckLogin cl=new CheckLogin();
	public void doGet(HttpServletRequest req, HttpServletResponse res)
			throws IOException, ServletException {
		try{
			doPost(req, res);
		}
		catch(Exception e){
			
		}
	}///do get///////
	public void doPost(HttpServletRequest req, HttpServletResponse res){
		
		try {
			HttpSession ses=req.getSession();

			System.out.println("Servlet called!!");
			String pd[]=new String[2];
			BeanUtility.populateBean(lfb, req);
			String enpass=null;
			String mailid=null;
			String email_id=lfb.getUsername();
			String pass=lfb.getPassword();
			enpass=e.encryptPassword(pass);
			//System.out.println("Username is====="+email_id);
			//System.out.println("encrypted password is"+enpass);
			pd=cl.login(email_id, enpass);
			if(pd!=null){
				fname=pd[0];
				lname=pd[1];
				mailid=pd[2];
				ses.setAttribute("fname", fname);
				ses.setAttribute("lname", lname);
				ses.setAttribute("email", mailid);
System.out.println("email id is -------------->"+mailid);
				RequestDispatcher requestDispatcher = req
						.getRequestDispatcher("./index.jsp");
				requestDispatcher.forward(req, res);
				
			}
			else{
				System.out.println("the array is totally null !!!!!");
				RequestDispatcher requestDispatcher = req
						.getRequestDispatcher("./index.jsp");
				requestDispatcher.forward(req, res);
			}
			
			
			///System.out.println("fname is===="+fname+"lanem is ====="+lname);
		} catch (NoSuchAlgorithmException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		catch(Exception e){
			e.printStackTrace();

		}
		

	}
}
